/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.CategoryDAL;
import client.dal.CustomerDAL;
import client.dal.LevelJobDAL;
import client.dal.LocationDAL;
import client.dal.RecruitmentDAL;
import client.entity.Account;
import client.entity.Category;
import client.entity.Customer;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Recruitment;
import client.entity.RecruitmentCategory;
import client.entity.RecruitmentLocation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class CustomerCreateProfile implements Serializable {

    private Customer cus;
    private Recruitment rec;
    private ArrayList<Category> cate;
    private ArrayList<String> subCate;
    private ArrayList<String> loc_temp;
    private ArrayList<Location> loc;
    private ArrayList<String> subLoc;
    private Date date_temp;
    private String number_temp;
    private String joj_temp = "1";
    public String lv_temp;

    public String getLv_temp() {
        return lv_temp;
    }

    public void setLv_temp(String lv_temp) {
        this.lv_temp = lv_temp;
    }

    public ArrayList<LevelJob> getListLJ() {
        return new LevelJobDAL().loadLevelJob();
    }

    public String getJoj_temp() {
        return joj_temp;
    }

    public void setJoj_temp(String joj_temp) {
        this.joj_temp = joj_temp;
    }

    public Date getDate_temp() {
        return date_temp;
    }

    public void setDate_temp(Date date_temp) {
        this.date_temp = date_temp;
    }

    public String getNumber_temp() {
        return number_temp;
    }

    public void setNumber_temp(String number_temp) {
        this.number_temp = number_temp;
    }

    public ArrayList<String> getSubLoc() {
        if (subLoc == null) {
            subLoc = new ArrayList<String>();
        }
        return subLoc;
    }

    public void setSubLoc(ArrayList<String> subLoc) {
        this.subLoc = subLoc;
    }

    public ArrayList<String> getLoc_temp() {
        return loc_temp;
    }

    public void setLoc_temp(ArrayList<String> loc_temp) {
        this.loc_temp = loc_temp;
    }

    public ArrayList<Location> getLoc() {
        return new LocationDAL().listLoca();
    }

    public void setLoc(ArrayList<Location> loc) {
        this.loc = loc;
    }

    public Customer getCus() {
        if (cus == null) {
//            cus = new CustomerDAL().loadCustomerByAccountID("27");
            cus=new Customer();
            int accId = Integer.parseInt(getParam("accId"));
            cus = new CustomerDAL().loadCustomerByAccountID(accId);
        }
        return cus;
    }

    public void setCus(Customer cus) {
        this.cus = cus;
    }

    public Recruitment getRec() {
        if (rec == null) {
            rec = new Recruitment();
        }
        return rec;
    }

    public void setRec(Recruitment rec) {
        this.rec = rec;
    }
    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public ArrayList<String> getSubCate() {
        if (subCate == null) {
            subCate = new ArrayList<String>();
        }
        return subCate;
    }

    public void setSubCate(ArrayList<String> subCate) {
        this.subCate = subCate;
    }

    /**
     * Creates a new instance of CustomerCreateProfile
     */
    public CustomerCreateProfile() {
    }

    public String getParam(String param) {

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);

    }
    private ArrayList<String> cate_temp;

    public ArrayList<String> getCate_temp() {
        if (cate_temp == null) {
            cate_temp = new ArrayList<String>();
        }
        return cate_temp;
    }

    public void setCate_temp(ArrayList<String> cate_temp) {
        this.cate_temp = cate_temp;
    }

    public ArrayList<Category> getCate() {
        return new CategoryDAL().listCate();
    }

    public void setCate(ArrayList<Category> cate) {
        this.cate = cate;
    }

    public String moveTo2() {
        return "createProfileStep2.xhtml?faces-redirect=true";
    }

    public void insertCate() {
        if (subCate.size() < 3) {
            for (String s : cate_temp) {
                subCate.add(s);
            }
        }
    }

    public void deleteCate(String s) {
        for (int i = 0; i < subCate.size(); i++) {
            if (s.equals(subCate.get(i))) {
                subCate.remove(i);
            }
        }
    }

    public void insertLoc() {
        if (subLoc.size() < 3) {
            for (String s : loc_temp) {
                subLoc.add(s);
            }
        }
    }

    public void deleteLoc(String s) {
        for (int i = 0; i < subLoc.size(); i++) {
            if (s.equals(subLoc.get(i))) {
                subLoc.remove(i);
            }
        }
    }

    public void upload() {
        if (file != null) {

            try {
                InputStream in = file.getInputstream();
                OutputStream out = new FileOutputStream(new File(FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") + "images\\Logo\\" + file.getFileName()));
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                in.close();
                out.flush();
                out.close();

            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public String customerCreateProfile() {
        if (subCate.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage("formId:select1", new FacesMessage("   * Desired Category is required"));
            return "";
        }
        if (subLoc.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage("formId:select2", new FacesMessage("   * Desired Location is required"));
            return "";
        }
        try {
            Integer.parseInt(number_temp);
            if (file != null) {
                cus.setLogo(file.getFileName());
            }
            new CustomerDAL().updateCustomer(cus);
            LevelJob lv = new LevelJobDAL().getLevelJobByID(lv_temp);
            rec.setLevelJob(lv);
            rec.setCustomer(cus);
            DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
            rec.setExpritedDate(df.format(date_temp));
            rec.setNumber(Integer.parseInt(number_temp));
            boolean toj = false;
            if (joj_temp.equals("1")) {
                toj = true;
            }
            rec.setTypeOfJob(toj);
            ArrayList<RecruitmentCategory> listRC_temp = new ArrayList<RecruitmentCategory>();
            for (String s : subCate) {
                RecruitmentCategory rc = new RecruitmentCategory(rec, listCateConvert(s));
                listRC_temp.add(rc);
            }
            ArrayList<RecruitmentLocation> listRL_temp = new ArrayList<RecruitmentLocation>();
            for (String ss : subLoc) {
                RecruitmentLocation rl = new RecruitmentLocation(rec, listLocaConvert(ss));
                listRL_temp.add(rl);
            }
            new RecruitmentDAL().insertRecrui(rec);
            new RecruitmentDAL().insertReCate(listRC_temp);
            new RecruitmentDAL().insertReLoca(listRL_temp);
            return "customerProfile.xhtml?faces-redirect=true";
        }catch(Exception ex){
            FacesContext.getCurrentInstance().addMessage("formId:numTemp", new FacesMessage("   * Number of recruitment is invalid"));
            return "";
        }
    }

    public Category listCateConvert(String s) {
        ArrayList<Category> list_temp = new CategoryDAL().listCate();
        for (Category c : list_temp) {
            if (s.equals(c.getCategoryName())) {
                return c;
            }
        }
        return null;
    }

    public Location listLocaConvert(String s) {
        ArrayList<Location> list_temp = new LocationDAL().listLoca();
        for (Location l : list_temp) {
            if (s.equals(l.getName())) {
                return l;
            }
        }
        return null;
    }
}
